<?php
	require('captcha.doc.php');
	//发送短信验证码
	$app->post(
		'/captcha',
		function() use($app, $db2){
			$data = $app->request()->post();
			$echo['error'] = "";
			if($data && isset($data['mobile']) && isset($data['type'])){
				$cap = $db2->select(1, 'tb_captcha', '*', array(
					'mobile' => $data['mobile'],
					'type' => $data['type'],
					'state' => 0,
					'creatdt[<=]' => date('Y-m-d H:i:s'),
					'invaliddt[>=]' => date('Y-m-d H:i:s')
				));
				if($cap == NULL){
					$cap['captcha'] = rand(100000, 999999);
					$db2->insert('tb_captcha', array(
						'mobile' => $data['mobile'],
						'captcha' => $cap['captcha'],
						'type' => $data['type'],
						'creatdt' => date('Y-m-d H:i:s'),
						'invaliddt' => date('Y-m-d H:i:s', strtotime('+30 minute'))
					));
				}
				switch($data['type']){
					case 11:
					case 21:
						$data['type'] = '注册';
						break;
					case 12:
					case 22:
						$data['type'] = '找回密码';
						break;
					case 13:
					case 23:
						$data['type'] = '修改支付密码';
						break;
					case 14:
					case 24:
						$data['type'] = '绑定提现账号';
						break;
				}
				//发送短信
				require_once('api/alidayu/TopSdk.php');
				date_default_timezone_set('Asia/Shanghai'); 
			
				$c = new TopClient;
				$c = new TopClient;
				$c->appkey = "23378456";
				$c->secretKey = "f76b08a66d6e21c7485908544336c60e";
				$req = new AlibabaAliqinFcSmsNumSendRequest;
				$req->setExtend($data['mobile']);
				$req->setSmsType("normal");
				$req->setSmsFreeSignName("阿达健身");
				$req->setSmsParam("{\"type\":\"".$data['type']."\",\"captcha\":\"".$cap['captcha']."\"}");
				$req->setRecNum($data['mobile']);
				$req->setSmsTemplateCode("SMS_10275127");
				$resp = $c->execute($req);
				if($resp->result->success == 'true'){
					$echo['captcha'] = $cap['captcha'];
				}else{
					$echo['error'] = '短信发送失败，请稍后再试';
					$echo['errorInfo'] = '错误代码：'.$resp->sub_code.'，错误信息：'.$resp->sub_msg;
				}
			}else{
				$echo['error'] = '数据缺失，请检查';
			}
			echo json_encode($echo);
		}
	);
?>